package rs.etf.kdp.client;

import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.io.ObjectOutputStream;

import javax.swing.JScrollPane;

public class RequestedJobsFromFile extends JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5791102683741730275L;
	RequestedJobsCollection req;
	private JTable table;
	private JButton btnRequestResult;
	private JFileChooser resFile;
	private ClientGUI gui;

	/**
	 * Create the dialog.
	 */
	public RequestedJobsFromFile(RequestedJobsCollection reqc, ClientGUI cgi) {
		setTitle("Requested Jobs from this computer\r\n");
		req = reqc;
		resFile = new JFileChooser();
		gui = cgi;
		setBounds(100, 100, 457, 278);
		getContentPane().setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 11, 412, 175);
		getContentPane().add(scrollPane);
		
		table = new JTable();
		scrollPane.setViewportView(table);
		table.setModel(new DefaultTableModel(
			getFromFile(),
			new String[] {
				"ID", "Name", "Status"
			}
		));
		
		JButton btnOk = new JButton("OK");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
			}
		});
		btnOk.setBounds(336, 203, 87, 23);
		getContentPane().add(btnOk);
		
		btnRequestResult = new JButton("Request result");
		btnRequestResult.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				onRequestClicked();
			}
		});
		btnRequestResult.setBounds(201, 203, 125, 23);
		getContentPane().add(btnRequestResult);
	}
	
	private Object[][] getFromFile()
	{
		List<RememberedJob> list = req.getFromFile();
		int inlist = list.size();
		Object[][] obj = new Object[inlist][3];
		
		for(int i=0; i<inlist; i++)
		{
			long id;
			String name;
			String status;
			RememberedJob rj = list.get(i);
			id = rj.getJobId();
			name = rj.getJobName();
			status = rj.getJobStatus();
			obj[i][0] = id;
			obj[i][1] = name;
			obj[i][2] = status;
		}
		
		return obj;
	}
	
	public void onRequestClicked()
	{
		ObjectOutputStream output = gui.output;
		File result = getFileForResult();
		int row = table.getSelectedRow();
		Long id = (Long) table.getValueAt(row, 0);
		
		Sender.FileLocation f = new Sender.FileLocation(id, result);
		Sender.fileLocations.add(f);
		
		try {
			output.writeObject("oldjob " + id);
		} catch (IOException e) {
			System.out.println("Unable to request old job");
		}
	}
	
	private File getFileForResult()
	{
		File ret = null;
		int retval = resFile.showSaveDialog(this);
		if(retval == JFileChooser.APPROVE_OPTION)
		{
			ret = resFile.getSelectedFile();
			
		}
		
		return ret;
	}
}
